package com.ttj.service.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ttj.domain.oracle.OrderSell;
import com.ttj.mapper.oracle.OrderSellMapper;
import com.ttj.service.OrderSellService;
import com.ttj.vo.BaseRes;
import com.ttj.vo.OrderSellVo;

/**
 * 兑换管理模块
 * @author ASia
 *
 */
@Service
public class OrderSellServiceImpl implements OrderSellService {
	
	@Resource
	private OrderSellMapper orderSell;
	
	@Override
	public BaseRes<OrderSellVo> index(String username) {
		int unCheckNum = this.unCheck(username);
		int replyedNum = this.replyed(username);
		OrderSellVo sellVo = new OrderSellVo();
		sellVo.setUncheckNum(unCheckNum);
		sellVo.setReplyedNum(replyedNum);
		BaseRes<OrderSellVo> baseRes = new BaseRes<OrderSellVo>();
		baseRes.setData(sellVo);
		return baseRes;
	}

	
	@Override
	public int unCheck(String rperson) {
		return orderSell.unCheck(rperson);
	}

	@Override
	public int replyed(String rperson) {
		return orderSell.replyed(rperson);
	}

	@Override
	public BaseRes<List<OrderSell>> orderSellList(String username,Integer oStatus) {
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		map.put("oStatus", oStatus);
		map.put("rperson", username);
		List<OrderSell> sellList = orderSell.orderSellList(map);
		for (OrderSell orderSell : sellList) {
			String addr = orderSell.getAddr() == null
					|| orderSell.getAddr().length() < 8 ? orderSell.getAddr()
					: orderSell.getAddr().substring(8);
			String oindate = orderSell.getoIndate() == null ? "" : sdf
					.format(orderSell.getoIndate());// 订单创建时间
			String rDate = orderSell.getrDate() == null ? "" : sdf
					.format(orderSell.getrDate());// 订单应答时间
			String lCTime = orderSell.getlCTime() == null ? "" : sdf
					.format(orderSell.getlCTime());// 订单送货完成时间
			orderSell.setRdate(rDate);// 订单应答时间
			orderSell.setLcdate(lCTime);// 订单送货完成时间
			orderSell.setOindate(oindate);// 订单创建时间
			orderSell.setAddr(addr);// 地址
		}
		BaseRes<List<OrderSell>> baseRes = new BaseRes<List<OrderSell>>();
		baseRes.setData(sellList);
		return baseRes;
	}


	@Override
	public BaseRes<List<OrderSell>> getOrderSellDetail(Long orderId,Integer oStatus) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		map.put("oStatus", oStatus);
		List<OrderSell> sellDetail = orderSell.getOrderSellDetail(map);
		for (OrderSell orderSell : sellDetail) {
			String addr = orderSell.getAddr() == null
					|| orderSell.getAddr().length() < 8 ? orderSell.getAddr()
					: orderSell.getAddr().substring(8);
			orderSell.setAddr(addr);// 地址
		}
		BaseRes<List<OrderSell>> baseRes = new BaseRes<List<OrderSell>>();
		baseRes.setData(sellDetail);
		return baseRes;
	}

	@Override
	public BaseRes<List<OrderSell>> getOrderSellDetailList(Long orderId) {
		List<OrderSell> sellDetailList = orderSell.getOrderSellDetailList(orderId);
		BaseRes<List<OrderSell>> baseRes = new BaseRes<List<OrderSell>>();
		baseRes.setData(sellDetailList);
		return baseRes;
	}


	@Override
	public int reply(Long orderId) {
		return orderSell.reply(orderId);
	}

	public int delivery(Long orderId){
		return orderSell.delivery(orderId);
	}

}
